package com.example.jeong.yuejian10.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RadioButton;

import com.example.jeong.yuejian10.R;
import com.jude.rollviewpager.RollPagerView;
import com.jude.rollviewpager.adapter.StaticPagerAdapter;
import com.jude.rollviewpager.hintview.ColorPointHintView;

//首页
public class ShouyeFragment extends Fragment implements View.OnClickListener{

    // 声明变量
    private View view = null;
    private ImageView imagesearch;//搜索
    private RollPagerView rollPVshouye = null;

    private RadioButton activityRb;
    private RadioButton professorRb;
    private RadioButton matchRb;

    private Fragment activityFragment;
    private Fragment professorFragment;
    private Fragment matchFragment;

    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view =  inflater.inflate(R.layout.tab03, container, false);
        initViews();

        initEvents();
        return view;
    }
    private void initViews(){
        imagesearch = (ImageView) view.findViewById(R.id.shouye_pic);
        rollPVshouye = (RollPagerView) view.findViewById(R.id.rollPV_shouye);
        activityRb = (RadioButton) view.findViewById(R.id.shouye_activity);
        professorRb = (RadioButton) view.findViewById(R.id.shouye_professor);
        matchRb = (RadioButton) view.findViewById(R.id.shouye_match);
    }
    //轮播图
    public void initEvents() {

        //跳转到搜索页面

        rollPVshouye.setPlayDelay(3000);
        //设置图片切换动画时间
        rollPVshouye.setAnimationDurtion(500);
        //设置指示器：

        rollPVshouye.setHintView(new ColorPointHintView(getActivity(),
                getResources().getColor(R.color.colorPrimary),
                Color.WHITE));
        //设置适配器
        rollPVshouye.setAdapter(new RollPagerAdapter());
        //设置每一个图片的点击事件


        //首页三个列表
        //精选活动页
        activityRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    activityFragment = new ActivityFragment();
                    replaceFragment(activityFragment);
                }
            }
        });
        //活动大师
        professorRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    professorFragment = new ProfessorFragment();
                    replaceFragment(professorFragment);
                }

            }
        });
        //最近比赛
        matchRb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    matchFragment = new MatchFragment();
                    replaceFragment(matchFragment);
                }
            }
        });
        fragmentManager=getChildFragmentManager();
        activityRb.setChecked(true);

    }
    private void replaceFragment(Fragment fragment) {
        transaction=fragmentManager.beginTransaction();
        transaction.replace(R.id.fragment_holder,fragment);
        transaction.commit();
    }
    public ShouyeFragment(){

    }

    private int images[] = {
            R.drawable.shouye_1,
            R.drawable.shouye_2,
            R.drawable.shouye_3
    };

    @Override
    public void onClick(View view) {

    }

    //设置适配器
    private class RollPagerAdapter extends StaticPagerAdapter{

        @Override
        public View getView(ViewGroup container, int position) {
            ImageView view = new ImageView(container.getContext());
            //设置图片资源
            view.setImageResource(images[position]);
            //设置高度和宽度
            view.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT));
            //设置拉伸方式
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            return view;
        }
        @Override
        public int getCount() {
            return images.length;
        }
    }


}


